﻿using System;
using System.Collections.Generic;
using System.Reflection;
using MyUtils;

namespace FitAutomation.FitProcess
{
	class FitProcessHost : IFitProcessHost
	{
		readonly Dictionary<string, object> _map = new Dictionary<string, object>();

		public void NewObject<T>(string objName)
		{
			NewObject(objName, typeof(T));
		}

		public void NewObject(string objName, Type type)
		{
			_map.Add(objName, Activator.CreateInstance(type));
		}

		public object PerformReflect(string objectName, string memberName, object[] args)
		{
			try
			{
				return _map[objectName].Reflect(memberName, args ?? new object[0]);
			}
			catch (Exception ex)
			{
				while (ex is TargetInvocationException && ex.InnerException != null)
				{
					ex = ex.InnerException;
				}
				return new ExceptionPackage {Exception = ex.ToString()};
			}
		}

	}
}
